package bud.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.support.RequestContextUtils;

import bud.vo.BoardFormDTO;
import bud.vo.BoardQADTO;
import bud.web.boardqa.service.IBoardqaService;
import bud.web.boardqa.service.IBoardqaService.BoardqaResult;


@Controller
public class BoardqaController {
	@Inject
	IBoardqaService service;
	
	@RequestMapping("boardqaList")
	public String boardqaList(String msg, Model model,
			@ModelAttribute("formDTO") BoardFormDTO<BoardQADTO> formDTO) {
		if(formDTO.getCurrentPage()==null) {
			formDTO.setCurrentPage(1);
		}
		formDTO.setScreen_size(15);
		formDTO.setBlock_size(7);
		formDTO.setTotalRecords(service.getBoardqaCount(formDTO));
		
		model.addAttribute("msg", msg);

		//페이징 처리를 위한 어트리뷰트를 스코프에 추가
		List<BoardQADTO> boardqaList = service.getBoardqaList(formDTO);
		formDTO.setDataList(boardqaList);
		return "boardqa/boardqaList";
	}
	
	@RequestMapping("boardqaInsert")
	public String boardqaInsert(@ModelAttribute("boardqa")BoardQADTO boardqa,
			BindingResult error, 
			Model model) {
		String goPage=null;
		if(error.hasErrors()) {
			model.addAttribute("msg", "알 수 없는 오류가 발생, 잠시 후 다시 시도해주세요.");
			goPage = "boardqa/boardqaForm";
		} else {
			BoardqaResult result = service.createBoardqa(boardqa);
			if(result == BoardqaResult.OK) {
				model.addAttribute("msg", "정상 등록 완료");
				goPage = "redirect:/boardqaList.do";
			} else {
				model.addAttribute("msg", "알 수 없는 오류가 발생, 잠시 후 다시 시도해주세요.");
				goPage = "boardqa/boardqaForm";
			}
		}
		return goPage;
	}
	
	@RequestMapping("boardqaForm")
	public String boardqaForm() {
		return "boardqa/boardqaForm";
	}
	
	@RequestMapping("boardqaView")
	public String boardqaView(String msg, int qa_no, Model model, HttpServletRequest request) {
		BoardQADTO boardqa = service.getBoardqa(qa_no);
		if(boardqa == null) {
			msg="글번호에 해당하는 게시글이 없습니다.";
			return "boardqa/boardqaList";
		}
		
		model.addAttribute("boardqa", boardqa);
		model.addAttribute("msg", msg);
		return  "boardqa/boardqaView";
	}
	
	@RequestMapping("boardqaEdit")
	public String boardqaEdit(String msg, @RequestParam("qa_no") int qa_no,
			Model model) {
		BoardQADTO boardqa = service.getBoardqa(qa_no);
		if(boardqa == null) {
			msg="글번호에 해당하는 게시글이 없습니다.";
			return "boardqa/boardList";
		}
		model.addAttribute("boardqa", boardqa);
		model.addAttribute("msg", msg);
		return "boardqa/boardqaEdit";
	}
	
	@RequestMapping("boardqaUpdate")
	public String boardqaUpdate(@ModelAttribute("boardqa") BoardQADTO boardqa, Model model) {
		BoardqaResult result = service.modifyBoardqa(boardqa);
		String goPage=null;
		String msg=null;
		if(result==BoardqaResult.OK) {
			msg="수정 완료";
			model.addAttribute("msg", msg);
			goPage="redirect:/boardqaView.do?qa_no="+boardqa.getQa_no();
		} else if(result==BoardqaResult.INVALIDPASSWORD) {
			msg="비밀번호 오류, 확인 후 다시 시도해주세요.";
			model.addAttribute("msg", msg);
			goPage ="redirect:/boardqaEdit.do?qa_no="+boardqa.getQa_no();
		} else {
			msg="알 수 없는 오류, 잠시 후 다시 시도해주세요.";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardqaEdit.do?qa_no="+boardqa.getQa_no();
		}
		return goPage;
	}
	
	@RequestMapping("boardqaDelete") 
	public String boardqaDelete(@ModelAttribute("boardqa") BoardQADTO boardqa,
			HttpServletResponse response, HttpServletRequest request, Model model) throws IOException {
		if(boardqa.getQa_no()==null) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST, "삭제글 번호가 없습니다");
			return null;
		}
		BoardqaResult result = service.removeBoardqa(boardqa);
		String goPage = null;
		String msg = null;
		if(result == BoardqaResult.OK) {
			msg="삭제 완료";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardqaList.do";
		} else if(result==BoardqaResult.INVALIDPASSWORD) {
			msg="비밀번호 오류, 확인 후 다시 시도해주세요";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardqaEdit.do?qa_no="+boardqa.getQa_no();
		} else {
			msg="알수없는 오류로 정상처리 불가능, 잠시 후 다시 시도해주세요";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardqaView.do?qa_no="+boardqa.getQa_no();
		}
		return goPage;
	}
	
	@RequestMapping("boardqaFormRe")
	public String boardqaFormRe(int qa_no, Model model) {
		BoardQADTO boardqa = service.getBoardqa(qa_no);
		int depthCount = service.getBoardqaDepthCount(boardqa);
		model.addAttribute("depthCount",depthCount);
		model.addAttribute("boardqa",boardqa);
		return "boardqa/boardqaFormRe";
	}
}


